/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
  <file> istr_fd.h </file>
  <brief>
    Input manipulating procedures from unix file descriptor or memory ranges.
    This file is part of ULS, Unified Lexical Scheme.
  </brief>
  <author>
    Stanley J. Hong <babel96@gmail.com>, June 2011.
  </author>
*/

#ifndef __ISTR_FD_H__
#define __ISTR_FD_H__

#include "uls_const.h"
#include "csz_stream.h"

#ifdef __cplusplus
extern "C" {
#endif

#define ISTR_FD_BUFSIZ  1023

#define ISTR_FD_EOF       0x01
#define ISTR_FD_ERR       0x02
#define ISTR_FD_AUTOCLOSE 0x04

typedef int (*istr_fd_checker_t)(uls_char_t* buf, int n);
struct _istr_fd {
	uls_flags_t flags;
	int  fd;
	uls_char_t *raw_buf;
	uls_char_t *raw_bufptr;
	int  raw_bufsiz, raw_buf_bytes;
	istr_fd_checker_t record_boundary_checker;
	csz_str_t  ubuf;
};

void istr_fd_init(istr_fd_t* fd_src, int fd);
void istr_fd_deinit(istr_fd_t* fd_src);
void istr_fd_reset(istr_fd_t* fd_src, int fd);

int istr_fd_readn(istr_fd_t *fd_src, uls_char_t *ptr0, int n);

int istr_fd_filler(uls_istream_t* istr,
	uls_char_t** p_line, uls_literal_t* lits, int* n_lits);
int istr_mem_filler(uls_istream_t* istr,
	uls_char_t** p_line, uls_literal_t* lits, int* ptr_n_lits);

int check_rec_boundary_bin_le(uls_char_t* buf, int n);
int check_rec_boundary_bin_be(uls_char_t* buf, int n);
int istr_binfd_filler(uls_istream_t* istr,
	uls_char_t** p_line, uls_literal_t* lits, int* n_lits);

uls_gettok_t find_fd_filler(
	int fd_type, int has_comm, istr_filler_t* p_fillproc, istr_fd_checker_t* p_subproc);

int refill_fd_src(istr_fd_t *fd_src, int n_bytes);

void init_line_in_fd_src(istr_fd_t *fd_src, const uls_char_t* line, int n_bytes);

#ifdef __cplusplus
}
#endif

#endif
